//
//  DDEditInvoiceInfoVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 2016/10/21.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class DDEditInvoiceInfoVC: LDBaseCustomizeVC {
    
    var editInfoView: DDEditInvoiceListView?
    var submitBtn:UIButton?
    var bottomView:LDBottomBtnView?
    
    var sendModel:DDInvoiceInfoModelList?
    
    private let actionBtnStr = "保存"
    private var listTitleArr = ["发票抬头","联系人","联系方式","所在地区","详细地址"]
    
    var tableView:LDBaseTableView?
    var pickV:LDBaseAddressPickerView?
    
    
    private var privenceArr = [String]()
    private var cityArr = [String]()
    private var districtArr = [String]()
    
    private var placeArr = [AnyObject]()
    
    private var provienceIndex = 0
    private var cityIndex = 0
    private var districtIndex = 0
    
    override func viewDidLoad() {
        
        initUI()
        requestNet()
    }
    
    func requestNet() {
        
        LDCheckPublicInfo.shareInstance.checkoutThreeRegion { [weak self] (data) in
            
            let proArr = data["list"] as! NSArray
            self?.placeArr = data["list"] as! [AnyObject]
            for item in proArr {
                self?.privenceArr.append(item["name"] as? String ?? "")
                
                let twoArr = item["city"] as! NSArray
                
                for item2 in twoArr {
                    
                    self?.cityArr.append(item2["name"] as! String)
                    self?.districtArr = item2["area"] as! Array<String>
                }
            }
            
//            print(self?.cityArr,self?.districtArr)
        }
    }
    
    func initUI() {
        
        view.backgroundColor = LDGolbalColor.BgGoundGray
        tableView = LDBaseTableView.init(frame: view.frame, style: UITableViewStyle.Plain)
        
        submitBtn = UIButton.setDetailsBtn(actionBtnStr, picName: nil, btnBackColor: LDGolbalColor.Green, titleColor: LDFontColor.White)
        
        tableView?.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
        
        bottomView = LDBottomBtnView(frame: CGRectMake(0,0,LDFrame.ScreenWidth,70))
        bottomView!.title = actionBtnStr

        tableView?.tableFooterView = bottomView
        
        tableView?.delegate = self
        tableView?.dataSource = self
    
        view.addSubview(tableView!)
        
    }
    
}

extension DDEditInvoiceInfoVC:UITableViewDelegate,UITableViewDataSource {
    
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.section == 0 && indexPath.row == 3 {
            
            
            pickV = LDBaseAddressPickerView()
            pickV!.pickView.delegate = self
            pickV!.pickView.dataSource = self
            pickV!.showview(self.view)
//            expectJobCityCell = tableView.cellForRowAtIndexPath(indexPath) as? baseMySettingCells
            pickV!.sendStatus({ [weak self] (title) in
                
                if title == "确定" {
                    
//                    let cityArr = self?.allCityData[(self?.provienceIndex)!]["cities"] as! Array<Dictionary<String,String>>
                    
//                    if cityArr.count >= self?.cityIndex {
                    
//                        let cityName = cityArr[(self?.cityIndex)!]["city_name"]
//                        let cityID:String = cityArr[(self?.cityIndex)!]["city_id"]!
//                        
//                        self?.addSendInfoDict["city_id"] = cityID
//                        self?.expectJobCityCell?.descLab?.text = cityName
//                    }
                    
                }
                
            })
        }
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 2
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
           
            return listTitleArr.count
        } else {
            
            return 1
        }
        
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        if indexPath.section == 0 {
            
            var cell = tableView.dequeueReusableCellWithIdentifier("DDEditInfoBaseCell") as? DDEditInfoBaseCell
            
            if cell == nil {
                
                cell = DDEditInfoBaseCell(style: UITableViewCellStyle.Default, reuseIdentifier: "DDEditInfoBaseCell")
            }
            
            cell?.titleLab?.text = listTitleArr[indexPath.row]
            
            if indexPath.row == 3 {
                
                cell?.imageMarkImgView?.hidden = false
                cell?.descLab?.enabled = false
            } else {
                
                cell?.imageMarkImgView?.hidden = true
            }
            
            return cell!
            
        } else {
            
            var cell = tableView.dequeueReusableCellWithIdentifier("LDBaseSingleLineCell") as? LDBaseSingleLineCell
            
            if cell == nil {
                
                cell = LDBaseSingleLineCell(style: UITableViewCellStyle.Default, reuseIdentifier: "LDBaseSingleLineCell")
            }
            cell!.statusBtn?.rac_signalForControlEvents(.TouchUpInside).subscribeNext({ (obj) in
                
                let btn = obj as! UIButton
                btn.selected = !btn.selected
                
            })
            
            cell?.title = "设置发票默认信息"
            return cell!
        }
       
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 48
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 25
    }
    
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let view = UIView()
        view.backgroundColor = LDGolbalColor.BgGoundGray
        
        return view
    }

}

extension DDEditInvoiceInfoVC:UIPickerViewDelegate,UIPickerViewDataSource {
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        
        return 3
    }
    
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        if component == 0 {
            
            return privenceArr.count
            
        } else if component == 1 {
            
            let selDict = placeArr[provienceIndex] as! NSDictionary
            let cityDictArr = selDict["city"] as! NSArray
            var cityStrArr = [String]()
            
            for item in cityDictArr {
                
                let cityName = item["name"] as! String
                cityStrArr.append(cityName)
                
            }
            
            return cityStrArr.count
        } else  {
            
            let selDict = placeArr[provienceIndex] as! NSDictionary//[AnyObject]//["city"]
            let selArea = selDict["area"] as! NSArray
            
            let distArr = selArea[cityIndex] as! [String]
            return  distArr.count
        }
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        if component == 0 {
            
            return privenceArr[row]
            
        } else if component == 1 {
            
            let selDict = placeArr[row] as! NSDictionary
            let cityDictArr = selDict["city"] as! NSArray
            var cityStrArr = [String]()
            
            for item in cityDictArr {
                
                let cityName = item["name"] as! String
                cityStrArr.append(cityName)
                
            }
            
            return cityStrArr[row]
            
        } else {
            
            let selDict = placeArr[provienceIndex] as! NSDictionary//["city"]
            let selArea = selDict[cityIndex] as! NSDictionary
            
            let distArr = selArea["area"] as! [String]
            
            return  distArr[row]
        }
    }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        if component == 0 {
            
            provienceIndex = row
        } else if component == 1 {
            
            cityIndex = row
        } else {
            
            districtIndex = row
        }
    }
}


